import Api from "../../ajax/index.js";

const initialUserInfo = {
	avatar: "",
	nickname: "",
	number: "",
	phone: "",
	postList: [],
	sex: "",
	token: "",
	userId: "",
};
export default {
	namespaced: true,
	state: {
		token: null,
		isLogin: false,
		userInfo: {
			...initialUserInfo,
		},
	},
	mutations: {
		setUserInfo(state, payload) {
			this.commit("user/setToken", payload.token);
			state.isLogin = true;
			// 将state.userInfo与payload合并
			state.userInfo = Object.assign(state.userInfo, payload);
			//缓存用户登陆状态
			uni.setStorageSync("userInfo", state.userInfo);
		},
		refreshUserInfo(state, payload) {
			state.userInfo = Object.assign(state.userInfo, payload);
			uni.setStorageSync("userInfo", state.userInfo);
		},
		setToken(state, payload) {
			if (payload) {
				state.token = payload;
				// 将token放入缓存
				uni.setStorageSync("token", payload);
			}
		},
		clearToken(state) {
			state.userInfo = {
				...initialUserInfo,
			};
			state.isLogin = false;
			state.token = null;
			uni.removeStorage({
				key: "userInfo",
			});
			uni.removeStorage({
				key: "token",
			});
		},
	},
	actions: {
		async addRecord({
			state,
			commit,
			rootState
		}, payload) {
			return new Promise(async (resolve, reject) => {
				try {
					await Api.user.addRecord({
						"openid": uni.getStorageSync("openid"),
						...payload,
					});
					resolve();
				} catch (e) {
					console.log("同步用户数据失败", e);
					reject(e);
				}
			});
		},
		async deleteRecord({
			state,
			commit,
			rootState
		}, payload) {
			console.log(payload)
			return new Promise(async (resolve, reject) => {
				try {
					await Api.user.deleteRecord({
						"openid": uni.getStorageSync("openid"),
						dataid: payload.dataid,
					});
					resolve();
				} catch (e) {
					console.log("同步用户数据失败", e);
					reject(e);
				}
			});
		},
		async syncTargetWeight({
			state,
			commit,
			rootState
		}, payload) {
			return new Promise(async (resolve, reject) => {
				try {
					await Api.user.syncTargetWeight({
						"openid": uni.getStorageSync("openid"),
						"targetWeight": payload
					});
					resolve();
				} catch (e) {
					console.log("同步用户数据失败", e);
					reject(e);
				}
			});
		},
		async syncUnit({
			state,
			commit,
			rootState
		}, payload) {
			return new Promise(async (resolve, reject) => {
				try {
					await Api.user.syncUnit({
						"openid": uni.getStorageSync("openid"),
						"unit": payload
					});
					resolve();
				} catch (e) {
					console.log("同步用户数据失败", e);
					reject(e);
				}
			});
		},
		async syncBodyHeight({
			state,
			commit,
			rootState
		}, payload) {
			return new Promise(async (resolve, reject) => {
				try {
					await Api.user.syncBodyHeight({
						"openid": uni.getStorageSync("openid"),
						"bodyHeight": payload
					});
					resolve();
				} catch (e) {
					console.log("同步用户数据失败", e);
					reject(e);
				}
			});
		},
		async syncSex({
			state,
			commit,
			rootState
		}, payload) {
			return new Promise(async (resolve, reject) => {
				try {
					await Api.user.syncSex({
						"openid": uni.getStorageSync("openid"),
						"sex": payload
					});
					resolve();
				} catch (e) {
					console.log("同步用户数据失败", e);
					reject(e);
				}
			});
		},
		async syncBirth({
			state,
			commit,
			rootState
		}, payload) {
			return new Promise(async (resolve, reject) => {
				try {
					await Api.user.syncBirth({
						"openid": uni.getStorageSync("openid"),
						"birthYear": payload
					});
					resolve();
				} catch (e) {
					console.log("同步用户数据失败", e);
					reject(e);
				}
			});
		},
		async syncRemind({
			state,
			commit,
			rootState
		}, payload) {
			return new Promise(async (resolve, reject) => {
				try {
					await Api.user.syncRemind({
						"openid": uni.getStorageSync("openid"),
					});
					resolve();
				} catch (e) {
					console.log("同步用户数据失败", e);
					reject(e);
				}
			});
		},
	},
};